﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{

    public class ObjData
    {
        public Matrix matrix;
        public Matrix vertexes;
        public Matrix result;
        public void init(int cnt)
        {
            matrix = new Matrix();
            vertexes = new Matrix();
            result = new Matrix();
            matrix.init(4);
            vertexes.init(cnt);
            result.init(cnt);
        }
        public int textureMapCount;
        public float[] textureMap;//2*n
        public int faceCount;
        public int[] faceVerticles;//3*n
        public int[] faceTexture;//3*n
        public int[] texture;//256*256
    }

    public class Class1
    {


        public static ObjData readObj(string file)
        {
            ObjData obj = new ObjData();
            FileStream fs = new FileStream(file + ".obj", FileMode.Open, FileAccess.Read);

            StreamReader sr = new StreamReader(fs);
            int verticleCount = 0;
            obj.textureMapCount = 0;
            obj.faceCount = 0;
            while (true)
            {
                string line = sr.ReadLine();
                if (line == null) break;

                line = line.Trim();

                if (line.StartsWith("v "))
                {// v  -0.0873 13.3457 -3.5094
                    verticleCount++;
                }
                else if (line.StartsWith("vt "))
                {//vt 0.0000 1.0000 0.0000
                    obj.textureMapCount++;
                }
                else if (line.StartsWith("f "))
                {//f 1/1 2/2 3/3 
                    obj.faceCount++;
                }
            }
            sr.Close();
            fs.Close();

            obj.init(verticleCount);
            obj.textureMap = new float[obj.textureMapCount * 2];//2*n
            obj.faceVerticles = new int[obj.faceCount * 3];//3*n
            obj.faceTexture = new int[obj.faceCount * 3];//3*n

            fs = new FileStream(file + ".obj", FileMode.Open, FileAccess.Read);

            sr = new StreamReader(fs);
            verticleCount = 0;
            int textureMapCount = 0;
            int faceCount = 0;
            while (true)
            {
                string line = sr.ReadLine();
                if (line == null) break;

                line = line.Trim();

                if (line.StartsWith("v "))
                {// v  -0.0873 13.3457 -3.5094
                    var s = line.Replace("  "," ").Split(' ');
                    obj.vertexes.m[verticleCount] = float.Parse(s[1]);
                    verticleCount++;
                    obj.vertexes.m[verticleCount] = float.Parse(s[2]);
                    verticleCount++;
                    obj.vertexes.m[verticleCount] = float.Parse(s[3]);
                    verticleCount++;
                    obj.vertexes.m[verticleCount] = 1;
                    verticleCount++;
                }
                else if (line.StartsWith("vt "))
                {//vt 0.0000 1.0000 0.0000
                    var s = line.Split(' ');
                    obj.textureMap[textureMapCount] = float.Parse(s[1]);
                    textureMapCount++;
                    obj.textureMap[textureMapCount] = float.Parse(s[2]);
                    textureMapCount++;
                }
                else if (line.StartsWith("f "))
                {//f 1/1 2/2 3/3 
                    var s = line.Split(' ');
                    var s1 = s[1].Split('/');
                    var s2 = s[2].Split('/');
                    var s3 = s[3].Split('/');
                    obj.faceVerticles[faceCount] = int.Parse(s1[0])-1;
                    obj.faceTexture[faceCount] = int.Parse(s1[1])-1;
                    faceCount++;
                    obj.faceVerticles[faceCount] = int.Parse(s2[0])-1;
                    obj.faceTexture[faceCount] = int.Parse(s2[1])-1;
                    faceCount++;
                    obj.faceVerticles[faceCount] = int.Parse(s3[0])-1;
                    obj.faceTexture[faceCount] = int.Parse(s3[1])-1;
                    faceCount++;
                }
            }
            sr.Close();
            fs.Close();


            Bitmap img = (Bitmap)Bitmap.FromFile(file + ".bmp");
            obj.texture = new int[256 * 256];

            for (int j = 0; j < 256; j++)
            {
                for (int i = 0; i < 256; i++)
                {
                    obj.texture[i + (255 - j) * 256] = img.GetPixel(i * img.Width / 256, j * img.Height / 256).ToArgb();
                }
            }

            return obj;
        }
    }
}
